package com.doit.day04

import scala.io.Source

/**
 * order.txt
 * order011,u001,300
*order012,u002,200
*order023,u006,100
*order056,u007,300
*order066,u003,500
*order055,u004,300
*order021,u005,300
*order014,u001,100
*order025,u005,300
*order046,u007,30
*order067,u003,340
*order098,u008,310
 *
 *user.txt
*u001,hls,22,fengjie
*u002,wangwu,31,lisi
*u003,zhangyanru,22,tananpengyou
*u004,laocao,26,fengyi
*u005,mengqi,12,nvmengqi
*u006,haolei,38,sb
*u007,wanghongjing,24,wife
*u009,wanghongjing,24,wife
 *
 *返回一个结果：order011  u001   300  hls  22   fengjie
 */
object _06_join案例 {
  def main(args: Array[String]): Unit = {
    //读文件
    val order: List[String] = Source.fromFile("data/order.txt").getLines().toList
    val user: List[String] = Source.fromFile("data/user.txt").getLines().toList
    //把他们放在一个集合里面  再压平   再根据uid分组   key就是uid value 就是集合里面放的是值    浩哥值得表扬，但是结局不行

    //首先将两个集合中的string 封装成元祖
    val order_tuple  = order.map(line => {
      //order025,u005,300
      val arr: Array[String] = line.split(",")
      (arr(1),(arr(0), arr(2)))
    })

    //这个会出问题，key相同导致value的值被覆盖从而丢失数据
/*    val map: Map[String, (String, String)] = order_tuple.toMap

    map.foreach(println)*/
    //将list集合转变成Map
    val user_tuple = user.map(line => {
      val arr: Array[String] = line.split(",")
      (arr(0), (arr(1), arr(2), arr(3)))
    })

    val user_map: Map[String, (String, String, String)] = user_tuple.toMap

    //left join
    order_tuple.map(tp=>{
      val user_id  = tp._1
      val user: (String, String, String) = user_map.getOrElse(user_id, ("未知", "未知", "未知"))
      (tp, user)
    }).foreach(println)


    //能够做出结果或，但是效率不高
  /*  for (elem1 <- order_tuple) {
      for (elem2 <- user_tuple) {

      }
    }

    //用map映射
    order_tuple.map(tp=>{
      val user_id: String = tp._2
      //针对user_tuple进行for循环 我能够得到user里面的每一个元素
      for (elem <- user_tuple) {
        if(user_id == elem._1){
          println((tp._1,tp._2,tp._3,elem._2,elem._3,elem._4))
        }
      }
    })*/

  }

}
